<template>
	<div class="user">
	  <el-form :inline="true" :model="userSearch" label-width="70px" style="min-width: 1200px;" class='searchForm'>
		<el-form-item label="用户名称">
			<el-input v-model="userSearch.username" placeholder="请输入" clearable/>
		</el-form-item>
		<el-form-item label="角色名称">
			<el-input v-model="userSearch.role" placeholder="请输入" clearable/>
		</el-form-item>
		<el-form-item label="邮箱">
			<el-input v-model="userSearch.email" placeholder="请输入" clearable/>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" @click="reset">重置</el-button>
			<el-button type="primary" @click="search">搜索</el-button>
		</el-form-item>
	  </el-form>
	  <el-dialog v-model="editDialogVisible" :title="title" style="width: 400px;" class='dialog'>
		  <el-form :model="userSave" label-position='right' label-width="70px">
			<el-form-item label="用户名">
				<el-input v-model="userSave.username" autocomplete="off" placeholder="请输入"/>
			</el-form-item>
			<el-form-item label="邮箱">
				<el-input v-model="userSave.email" autocomplete="off" placeholder="请输入"/>
			</el-form-item>
			<el-form-item label="密 码">
				<el-input v-model="userSave.password" autocomplete="off" placeholder="请输入" type="password" show-password/>
			</el-form-item>
			<el-form-item label="确认密码">
				<el-input v-model="userSave.password_confirm" autocomplete="off" placeholder="请输入" type="password" show-password/>
			</el-form-item>
			<el-form-item label="关联角色" >
				<el-select
					v-model="userSave.role"
					placeholder="请选择"
					filterable
					clearable
					  >
						<el-option v-for="role_obj in role_names" :label='role_obj.name' :value="role_obj.id" />
				</el-select>
			</el-form-item>
		  </el-form>
		  <template #footer>
			<span class="dialog-footer">
			  <el-button @click="editDialogVisible = false">取消</el-button>
			  <el-button type="primary" @click="save">
				确认
			  </el-button>
			</span>
		  </template>
	  </el-dialog>
	  <el-table :data="user_list.results" class='table'>
		<el-table-column label="序号" width="70" type="index"/>
		<el-table-column label="用户名称" prop="username" min-width="100"/>
		<el-table-column label="角色名" prop="role_name" min-width="120">
			<template #default="scope">
			  <el-link type="primary" @click='editRole(scope.row)'>{{ scope.row.role_name }}</el-link>
			</template>
		</el-table-column>
		<el-table-column label="邮箱" prop="email" min-width="120"/>
		<el-table-column label="创建人" prop="create_by_name"  min-width="150"/>
		<el-table-column label="更新人" prop="update_by_name"  min-width="150"/>
		<el-table-column label="创建时间" prop="create_time" sortable min-width="150"/>
		<el-table-column label="更新时间" prop="update_time" sortable min-width="150"/>
		<el-table-column align="center" min-width="180">
			<template #header>
				<el-button v-if='permission.has_add_permission' type='primary' @click="addUser">新增</el-button>
			</template>
			<template #default="scope">
			  <el-button v-if="permission.has_edit_permission" :icon="Edit" size="small" type='primary' @click="editUser(scope.row)">编辑</el-button
			  >
			  <el-button v-if="permission.has_delete_permission"
				@click="deleteUser(scope.row.id)"
				size="small"
				type="danger"
			  :icon="Delete"
				>删除</el-button
			  >
			</template>
		</el-table-column>
	  </el-table>
	  <div style="margin: 10px">
	  	<el-pagination
	  	  v-model:current-page="page_size_params.page"
	  	  v-model:page-size="page_size_params.size"
	  	  :page-sizes="[10, 20, 30, 50]"
	  	  :small="small"
	  	  :disabled="disabled"
	  	  :background="background"
	  	  :hide-on-single-page="false"
	  	  layout="total, sizes, prev, pager, next, jumper"
	  	  :total="user_list.count"
	  	  @size-change="handleSizeChange"
	  	  @current-change="handleCurrentChange"
	  	/>
	  </div>
	</div>
</template>

<script>
import {mapState, mapActions, mapGetters} from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import { View, Delete, Edit, Plus, EditPen} from '@element-plus/icons-vue'

export default{
	computed:{
		...mapState(['pathPermission']),
	},
	data() {
		return {
			permission: {},
			userSearch:{
				username: '',
				role: '',
				email: ''
			},
			page_size_params: {
				page: 1,
				size: 10,
			},
			count: 1,
			title: '新增用户',
			isAdd: true,
			editDialogVisible: false,
			user_list: [],
			ViewVisible: false,
			userSave:{
				id: '',
				username: '',
				email: '',
				password_confirm: '',
				password: '',
				role: '',
			},
			role_names: [],
		}
	},
	setup() {
		return {
			Edit,
			Delete,
			Plus,
			View,
			EditPen,
		}
	},
	methods:{
		...mapActions(['getRolePermission']),
		handleCurrentChange(){
			this.getUsers()
		},
		handleSizeChange(){
			this.getUsers()
		},
		search(){
			this.getUsers()
		},
		reset(){
			for(let key in this.userSearch){
				this.userSearch[key] = ''
			}
		},
		editRole(row_data){
			this.$router.push({path: '/user/roleEdit', query: {id: row_data.role}})
		},
		editUser(row_data){
			this.isAdd = false
			this.title = '编辑用户'
			this.userSave = row_data
			this.userSave.password = ''
			this.editDialogVisible = true
			this.getRoles()
		},
		addUser(){
			this.userSave = {}
			this.title = '新增用户'
			this.editDialogVisible = true
			this.isAdd = true
			this.getRoles()
		},
		save(){
			if (this.isAdd){
				this.createUser()
			}else{
				this.updateUser()
			}
		},
		async createUser(){
			const response = await this.$api.createUser(this.userSave)
			if(response.status === 201){
				this.editDialogVisible = false
				this.getUsers()
			}
		},
		async updateUser(){
			const response = await this.$api.updateUser(this.userSave.id, this.userSave)
			if(response.status === 200){
				this.editDialogVisible = false
				this.getUsers()
			}
		},
		async deleteUser(id){
			const response = await this.$api.deleteUser(id)
			if (response.status === 204){
				this.getRoles(Object.assign(this.userSearch, this.page_size_params))
				this.getUsers()
			}
		},
		async getUsers(){
			const response = await this.$api.getUsers(Object.assign(this.userSearch, this.page_size_params))
			if (response.status === 200){
				this.user_list = {...response.data}
			}
		},
		async getRoles(params){
			const response = await this.$api.getRoles(params)
			if (response.status === 200){
				this.role_names = response.data.results
			}
		},
	},
	created() {
		this.getRolePermission(this.pathPermission[this.$route.path]).then(res =>{
			this.permission = {...res.result}
		})
		this.getUsers()
	}
}
</script>

<style scoped>
	.user{
		width: 100%;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	.user .searchForm{
		width: 100%;
		flex: 1;
		background-color: white;
		padding: 15px 15px 0 15px;
		margin-bottom: 5px;
	}
	.user .el-form--inline .el-form-item{
		margin-bottom: 15px;
	}
	.user .table{
		background-color: white;
		flex: 1;
	}
	.searchForm .el-input{
		width: 150px;
	}
	.searchForm .el-select{
		width: 150px;
	}
	.dialog .el-input{
		width: 300px;
	}
	.dialog .el-select{
		width: 300px;
	}
	.form .el-select{
		width: 300px;
	}
	.el-button{
		width: 70px;
	}
	.el-icon{
		margin-left: 10px;
	}
	
</style>
